#include "stdafx.h"
#if 0 
#include "GameClient.h"
#include "Communicator.h"

GameClient::GameClient() : socket(io_service), buffer(1024)
{
    io_service.run();
}

void GameClient::send(std::vector<unsigned char>& data)
{
    boost::system::error_code writeError;
    boost::asio::write(socket, boost::asio::buffer(data), writeError);
    // TODO: handle write errors
}

void GameClient::send(void* data, unsigned int size)
{
    boost::system::error_code writeError;
    boost::asio::write(socket, boost::asio::buffer(data, size), writeError);
    // TODO: handle write errors
}

void GameClient::receiveKnown(void* buffer, unsigned int size)
{
    boost::system::error_code readError;
    boost::asio::read(socket, boost::asio::buffer(buffer, size), boost::asio::transfer_all(), readError);

    /*
    if (readError == boost::asio::error::eof)
        ; // TODO: handle connection reset by server 
    else if (readError)
        throw boost::system::system_error(readError); // Some other error.
    */
}

void GameClient::receiveUnknown(std::vector<unsigned char>& buffer)
{
    boost::system::error_code readError;
    /* Get the size of incoming data */
    int incomingSize = -1;
    boost::asio::read(socket, boost::asio::buffer(&incomingSize, sizeof(int)), boost::asio::transfer_all(), readError);
    /* read data */ 
    resizeIf(buffer, incomingSize);
    boost::asio::read(socket, boost::asio::buffer(&buffer[0], incomingSize), boost::asio::transfer_all(), readError);
}

void GameClient::connect()
{
    std::printf("Im a client\n");
    try 
    {
        tcp::resolver resolver(io_service);
        tcp::resolver::query query("localhost", "13");
        
        tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

        /* establish connection to socket */ 
        boost::asio::connect(socket, endpoint_iterator);

#if 0 
        for ( ;; )
        {
            std::printf("Connected to server\n");


            unsigned int inSize = 0;
            
            boost::system::error_code readError;

            // get the size of the incomming packet 
            /*socket.read_some(boost::asio::buffer(&inSize, sizeof(unsigned int)), readError);*/
            /*boost::asio::read(socket, boost::asio::buffer(&inSize, sizeof(unsigned int)), boost::asio::transfer_all(), readError);
            gsc->getInBuffer().resize(inSize);
            std::printf("sizeof data: %u\n", inSize);
            boost::asio::read(socket, boost::asio::buffer(gsc->getInBuffer()), boost::asio::transfer_all(), readError);
            gsc->deSerializeState();*/
            /*std::printf("data: "*/
            /*socket.read_some(boost::asio::buffer(gsc->getInBuffer(), sizeof(unsigned int)), readError);*/

            if (readError == boost::asio::error::eof)
                break; // Connection closed cleanly by peer.
            else if (readError)
                throw boost::system::system_error(readError); // Some other error.
        }
#endif
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}
#endif 